import { ReactNode, HTMLAttributes, CSSProperties } from 'react';
import type { SearchResult, SearchSuggestion, SearchFilters } from './search';
import type { RecommendationResult } from './recommendation';

// 基础组件 Props
export interface BaseComponentProps {
  className?: string;
  style?: CSSProperties;
  children?: ReactNode;
  testId?: string;
  id?: string;
}

// 加载状态类型
export type LoadingState = 'idle' | 'loading' | 'success' | 'error';

// 尺寸类型
export type Size = 'small' | 'medium' | 'large';

// 变体类型
export type Variant = 'primary' | 'secondary' | 'success' | 'warning' | 'error' | 'info';

// 搜索输入组件 Props
export interface SearchInputProps extends BaseComponentProps {
  placeholder?: string;
  value?: string;
  defaultValue?: string;
  onSearch: (query: string) => void;
  onSuggestionSelect?: (suggestion: SearchSuggestion) => void;
  onChange?: (value: string) => void;
  onFocus?: (e: React.FocusEvent<HTMLInputElement>) => void;
  onBlur?: (e: React.FocusEvent<HTMLInputElement>) => void;
  suggestions?: SearchSuggestion[];
  loading?: boolean;
  disabled?: boolean;
  enableVoiceSearch?: boolean;
  enableImageSearch?: boolean;
  debounceMs?: number;
  maxLength?: number;
  autoFocus?: boolean;
  clearable?: boolean;
  size?: Size;
}

// 搜索建议组件 Props
export interface SearchSuggestionsProps extends BaseComponentProps {
  suggestions: SearchSuggestion[];
  onSelect: (suggestion: SearchSuggestion) => void;
  onClose?: () => void;
  visible?: boolean;
  loading?: boolean;
  maxItems?: number;
  highlightQuery?: string;
  position?: 'bottom' | 'top';
  selectedIndex?: number;
}

// 搜索结果组件 Props
export interface SearchResultsProps extends BaseComponentProps {
  results: SearchResult[];
  loading?: boolean;
  error?: string | null;
  onResultClick?: (result: SearchResult, index: number) => void;
  onLoadMore?: () => void;
  hasMore?: boolean;
  virtualScrolling?: boolean;
  itemHeight?: number;
  containerHeight?: number;
  highlightQuery?: string;
  showThumbnails?: boolean;
  layout?: 'list' | 'grid' | 'card';
}

// 搜索结果项组件 Props
export interface SearchResultItemProps extends BaseComponentProps {
  result: SearchResult;
  index: number;
  onClick?: (result: SearchResult, index: number) => void;
  highlightQuery?: string;
  showThumbnail?: boolean;
  layout?: 'list' | 'grid' | 'card';
}

// 搜索筛选器组件 Props
export interface SearchFiltersProps extends BaseComponentProps {
  filters: SearchFilters;
  onChange: (filters: SearchFilters) => void;
  onReset?: () => void;
  availableFilters?: {
    contentTypes?: string[];
    sources?: string[];
    languages?: string[];
    categories?: string[];
  };
  collapsed?: boolean;
  onToggleCollapse?: (collapsed: boolean) => void;
}

// 推荐列表组件 Props
export interface RecommendationListProps extends BaseComponentProps {
  recommendations: RecommendationResult[];
  loading?: boolean;
  error?: string | null;
  onItemClick?: (recommendation: RecommendationResult, index: number) => void;
  onFeedback?: (recommendationId: string, feedback: 'like' | 'dislike') => void;
  title?: string;
  showReason?: boolean;
  layout?: 'horizontal' | 'vertical' | 'grid';
  maxItems?: number;
}

// 虚拟滚动组件 Props
export interface VirtualScrollProps extends BaseComponentProps {
  items: any[];
  itemHeight: number | ((index: number) => number);
  containerHeight: number;
  renderItem: (item: any, index: number, style: CSSProperties) => ReactNode;
  overscan?: number;
  onScroll?: (scrollTop: number) => void;
  scrollToIndex?: number;
  scrollToAlignment?: 'start' | 'center' | 'end' | 'auto';
}

// 无限滚动组件 Props
export interface InfiniteScrollProps extends BaseComponentProps {
  hasMore: boolean;
  loading: boolean;
  onLoadMore: () => void;
  threshold?: number;
  loader?: ReactNode;
  endMessage?: ReactNode;
  scrollableTarget?: string;
}

// 懒加载图片组件 Props
export interface LazyImageProps extends BaseComponentProps {
  src: string;
  alt: string;
  placeholder?: string;
  fallback?: string;
  width?: number | string;
  height?: number | string;
  objectFit?: 'contain' | 'cover' | 'fill' | 'none' | 'scale-down';
  loading?: 'lazy' | 'eager';
  onLoad?: () => void;
  onError?: () => void;
  threshold?: number;
}

// 按钮组件 Props
export interface ButtonProps extends BaseComponentProps, Omit<HTMLAttributes<HTMLButtonElement>, 'className' | 'style'> {
  variant?: Variant;
  size?: Size;
  disabled?: boolean;
  loading?: boolean;
  icon?: ReactNode;
  iconPosition?: 'left' | 'right';
  fullWidth?: boolean;
  href?: string;
  target?: string;
  onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;
}

// 输入框组件 Props
export interface InputProps extends BaseComponentProps, Omit<HTMLAttributes<HTMLInputElement>, 'className' | 'style' | 'size' | 'prefix'> {
  type?: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url' | 'search';
  size?: Size;
  variant?: 'outlined' | 'filled' | 'standard';
  error?: boolean;
  helperText?: string;
  label?: string;
  required?: boolean;
  disabled?: boolean;
  loading?: boolean;
  prefix?: ReactNode;
  suffix?: ReactNode;
  clearable?: boolean;
  maxLength?: number;
  showCount?: boolean;
}

// 模态框组件 Props
export interface ModalProps extends BaseComponentProps {
  visible: boolean;
  onClose: () => void;
  title?: ReactNode;
  footer?: ReactNode;
  width?: number | string;
  height?: number | string;
  centered?: boolean;
  closable?: boolean;
  maskClosable?: boolean;
  keyboard?: boolean;
  destroyOnClose?: boolean;
  zIndex?: number;
  mask?: boolean;
  maskStyle?: CSSProperties;
  bodyStyle?: CSSProperties;
}

// 加载组件 Props
export interface LoadingProps extends BaseComponentProps {
  size?: Size;
  spinning?: boolean;
  tip?: string;
  delay?: number;
  indicator?: ReactNode;
}

// 错误边界组件 Props
export interface ErrorBoundaryProps extends BaseComponentProps {
  fallback?: ReactNode | ((error: Error, errorInfo: any) => ReactNode);
  onError?: (error: Error, errorInfo: any) => void;
  resetOnPropsChange?: boolean;
  resetKeys?: Array<string | number>;
}

// 表单字段组件 Props
export interface FormFieldProps<T = any> extends BaseComponentProps {
  name: string;
  label?: string;
  value?: T;
  defaultValue?: T;
  onChange?: (value: T) => void;
  onBlur?: () => void;
  error?: string;
  required?: boolean;
  disabled?: boolean;
  placeholder?: string;
  helperText?: string;
  size?: Size;
}
